package com.vhall.component.service.room.manager;

import com.vhall.component.plugin.common.constant.HttpConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * 房间组件mq类操作
 *
 * @author yuanzh
 * @date 2021/11/26
 */
@Slf4j
@Component
public class RoomMqManager {

    @Value("${vhall.bigdata.broadcast-record-name:bigdata-broadcast-record-stream}")
    private String broadcastRecordReportStreamName;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 房间关闭直播后，发送异步消息进行直播记录大数据上报处理
     *
     * @param roomExtendsId 房间扩展信息id，即开播记录id
     * @return 消息发送成功与否
     */
    public boolean sendBigDataBroadcastRecordReportMessage(Integer roomExtendsId) {
        // 发送消息
        try {
            StringRecord stringRecord = StreamRecords.string(Collections.singletonMap(HttpConstant.DATA, String.valueOf(roomExtendsId)))
                    .withStreamKey(broadcastRecordReportStreamName);
            RecordId recordId = stringRedisTemplate.opsForStream().add(stringRecord);
            log.info("export表插入数据的消息发送成功,[{}]", recordId);
            return true;
        } catch (Exception e) {
            // 为了不影响正常请求，不做异常抛出处理
            log.info("开播记录数据消息发送失败,[{}]", e.getMessage());
        }
        return false;
    }
}
